//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using OpenLS.Chart.UI;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    /// <summary>
    /// Interaction logic for SpreadsheetRibbon.xaml
    /// </summary>
    public partial class SpreadsheetRibbon: Ribbon
    {
        public SpreadsheetRibbon()
        {
            InitializeComponent();
#if DEBUG
            showDebug();
#endif
        }

        internal Range Selection
        {
            get
            {
                if (currentWorksheet == null)
                    return null;
                return currentWorksheet.Selection;
            }
        }

        private Worksheet currentWorksheet
        {
            get { return DataContext as Worksheet; }
        }

        private void showDebug()
        {
            foreach (TabItem item in ribbon.Items)
            {
                if (item.Header != null && item.Header.ToString() == "Debug")
                    item.Visibility = Visibility.Visible;
            }
        }

#if DEBUG
#endif

        private void onDebugTextBoxLoaded(object sender, object args)
        {
            var tb = (TextBox) sender;
            tb.Text = "Version = " + typeof (Worksheet).Assembly.FullName;
            //\\Debug.Listeners.Add(new TextboxTraceListener(tb));
        }

        private void onCheckInvariantClick(object sender, object args)
        {
            try
            {
                Workbook wb = SpreadsheetElement.GetWorkbook((DependencyObject) sender);
                
                //\\wb.CheckInvariant();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace, e.Message);
            }
        }


        private void onBugReport(object sender, object args)
        {
            var w = new BugReportWindow();
            w.ShowDialog();
        }



        private void onHomeTabSelected(object sender, RoutedEventArgs e)
        {
            var ti = (TabItem) sender;
            if (ti.Content != null)
                return;
            var timer = new DispatcherTimer(DispatcherPriority.ApplicationIdle);
            timer.Tick += delegate
                              {
                                  object o = new HomeTabContent();
                                  ti.Content = o;
                                  timer.Stop();
                              };
            timer.Start();
        }

        private static void insertTab(object sender, OpenLS.UI.GroupPanel tabContent)
        {
            try
            {
                var ti = (TabItem) sender;
                if (ti.Content != null)
                    return;
                ti.Content = tabContent;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                throw;
            }
        }

        private void onInsertTabSelected(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new CollaborativeModulesTabContent());
        }

        private void onPageLayoutTabSelected(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new PageLayoutTabContent());
        }

        private void onDataTabSelected(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new DataTabContent());
        }

        private void onViewTabSelected(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new ViewTabContent());
        }

        private void onFormulaTabSelected(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new FormulaTabContent());
        }

        private void onReviewTabSelected(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new ReviewTabContent());
        }

        private void onDesignTabSeleted(object sender, RoutedEventArgs e)
        {
            var ti = (TabItem) sender;
            if (ti.Content != null)
                return;
            ti.Content = new DesignTabResources()[ChartComponents.DesignTabKey];
        }

        private void onDimensionTabSeleted(object sender, RoutedEventArgs e)
        {
            insertTab(sender, new FileManagerTabContent());
        }

        private void onButtonClick(object sender, RoutedEventArgs e)
        {
            using (TextReader reader = new StreamReader(DebugTraceListener.Instance.FileName))
            {
                new Window {Content = new TextBox {Text = reader.ReadToEnd()}}.Show();
            }
        }

        private void onRemoveOneGroup(object sender, RoutedEventArgs e)
        {
            Button button = (Button) sender;
            Ribbon ribbon = WindowServices.GetAncestor<Ribbon>(button);
            foreach (TabItem tabItem in ribbon.Items)
            {
                OpenLS.UI.GroupPanel groupPanel = (OpenLS.UI.GroupPanel) tabItem.Content;
                if (groupPanel != null && groupPanel.Children.Count > 0)
                {
                    groupPanel.Children.RemoveAt(0);
                    break;
                }
            }
        }

        private void onRemoveStatusBar(object sender, RoutedEventArgs e)
        {
            SpreadsheetWindow w = (SpreadsheetWindow) Window.GetWindow((DependencyObject) sender);
            // TODO SJE: Replace by using DevXPRess DockManager
            // w.dockPanel.Children.RemoveAt(0);
        }

        private void onRemoveApplicationMenu(object sender, RoutedEventArgs e)
        {
            SpreadsheetWindow w = (SpreadsheetWindow)Window.GetWindow((DependencyObject)sender);
            w.ApplicationMenu = null;
        }

        private void onRemoveQAT(object sender, RoutedEventArgs e)
        {
            SpreadsheetWindow w = (SpreadsheetWindow)Window.GetWindow((DependencyObject)sender);
            w.QuickAccessToolbar = null;

        }

        private void onRemoveRibbon(object sender, RoutedEventArgs e)
        {
            SpreadsheetWindow w = (SpreadsheetWindow)Window.GetWindow((DependencyObject)sender);
            w.Ribbon = null;
        }
    }
}